﻿using BCCommon;
using BCCommon.Http;
using BCData.Common.BankCardPattern;
using BCDto.Common.BankCardDetection;
using BCDto.Common.SystemExceptionRecord;
using BCService.Common.ExceptionService;
using BCService.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BCService.CommonService.BankCardDetection
{
    public class BankCardPatternService : IBankCardPatternService
    {
       
        private readonly IBankCardPatternData bankCardPatternData;
        private readonly ISystemExceptionRecordService exceptionRecordService;

        public BankCardPatternService(IBankCardPatternData bankCardPatternData, ISystemExceptionRecordService exceptionRecordService)
        {
            this.bankCardPatternData = bankCardPatternData;
            this.exceptionRecordService = exceptionRecordService;
        }

        public IEnumerable<BankCardPatternDto> ALL()
        {
            var list = this.bankCardPatternData.ALL();
            IEnumerable<BankCardPatternDto> bankCardPatternDtos = list.Select(p => p.As<BankCardPatternDto>());
            return bankCardPatternDtos;
        }

        public BankCardTypeDto Match(string bankCardNo)
        {
            var list = ALL();

            foreach (BankCardPatternDto item in list)
            {
                if (Regex.IsMatch(bankCardNo, item.Reg))
                {
                    return new BankCardTypeDto {
                        bankCardNo = bankCardNo,
                        bankCode = item.BankCode,
                        cardType =item.CardType
                    };
                }                

            }
            return null;

        }

        public BankCardTypeDto AlipayBankCardMatch(string bankCardNo)
        {
            string url = string.Format("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo={0}&cardBinCheck=true", bankCardNo);



            Dictionary<string, string> headers = new Dictionary<string, string>();
           
            HttpRequestDto httpRequestDto = new HttpRequestDto()
            {
                Headers = headers,
                Url = url,
                Method =MethodType.GET,
                ContentType =FormatType.JSON,
                Encoding = Encoding.UTF8.ToString()               
            };
            try
            {
                HttpHandle http = new HttpHandle();
                /*匹配结果：validated==true匹配其他不匹配*/
                HttpResponseDto response = http.HttpSendRequest(httpRequestDto);
                //{"cardType":"DC","bank":"PSBC","key":"6221506020009066385","messages":[],"validated":true,"stat":"ok"}
                if (response.ResponseMessage.Contains("bank"))
                {
                    AlipayBankCardTypeDto alipayBankCardTypeDto = JsonConvert.DeserializeObject<AlipayBankCardTypeDto>(response.ResponseMessage);
                    return new BankCardTypeDto
                    {
                        bankCardNo = bankCardNo,
                        bankCode = alipayBankCardTypeDto.bank,
                        cardType = alipayBankCardTypeDto.cardType
                    };
                }
                else
                {
                    AlipayBankCardError alipayBankCardError = JsonConvert.DeserializeObject<AlipayBankCardError>(response.ResponseMessage);
                    SystemExceptionRecordDto systemExceptionRecordDto = new SystemExceptionRecordDto();
                    systemExceptionRecordDto.Data = response.ResponseMessage;
                    systemExceptionRecordDto.Name = "银行卡检测类型异常";
                    systemExceptionRecordDto.Message = "支付宝银行卡检测类型异常";
                    systemExceptionRecordDto.Source ="";
                    systemExceptionRecordDto.StackTrace = "";
                    systemExceptionRecordDto.HelpLink = "";
                    systemExceptionRecordDto.HResult = 0;
                    systemExceptionRecordDto.InnerException ="";

                    this.exceptionRecordService.Create(systemExceptionRecordDto);
                }
            }
            catch(Exception e) { 
                SystemExceptionRecordDto systemExceptionRecordDto = new SystemExceptionRecordDto();
                systemExceptionRecordDto.Data = JsonConvert.SerializeObject(httpRequestDto);
                systemExceptionRecordDto.Name = "银行卡检测类型异常";
                systemExceptionRecordDto.Message = "支付宝银行卡检测类型异常";
                systemExceptionRecordDto.Source = e.Source;
                systemExceptionRecordDto.StackTrace = e.StackTrace;
                systemExceptionRecordDto.HelpLink = e.HelpLink;
                systemExceptionRecordDto.HResult = e.HResult;
                systemExceptionRecordDto.InnerException = JsonConvert.SerializeObject(e.InnerException);

                this.exceptionRecordService.Create(systemExceptionRecordDto);
            }

            return null;

        }
    }
}
